package lib::Req;

# This file is part of noplagia.
#
# noplagia is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# noplagia is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# noplagia; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA  02111-1307 USA

use strict;
use lib::Context;
use Yahoo::Search;  # pour la recherche yahoo
use LWP::UserAgent; # pour la recherche google
use lib::TextTreatment;

require Exporter;
our ($VERSION,@ISA,@EXPORT);

$VERSION = 0.01;

=head1 NAME

lib::Req - Module qui fait les requetes aupres des moteurs de recherches

=head1 SYNOPSIS

  use lib::Req;

  my $response = request(
    {
        engine => "yahoo",
        count  => "10",
        lang   => "fr",
    }
  );

=head1 DESCRIPTION

    Ce module permet d'effectuer des requetes aupres des moteurs de recherches.
    Il retourne une reference a un tableau de hash.

=head1 FUNCTIONS

=over 2

=cut

@ISA    = qw(Exporter);
@EXPORT = qw(request);

=item request

$return = &request(
    {
        text   => "Mon texte a chercher"
        engine => "yahoo",
        count  => "10",
        lang   => "fr",
    }
);

Lance une requete vers le moteur de recherche et retourne un hashref I<$return>
Contenant les cles suivantes :

=over 4

=item url

=item title

=item summary

=item click

=back

=cut

sub request {
    my $in = shift;

    my $engine = $in->{engine} || "Yahoo";

    my $return;
    if ($engine =~ /Yahoo/i){
        $return = _req_yahoo($in);
    }
    if($engine =~ /Google/i){
        warn "Google non implemente..."
    }
    return $return;
}

sub _req_yahoo {
    my $in = shift;
    my $text_offset  = $in->{'text_offset'};
    my $count = $in->{'count'};
    my $lang  = $in->{'lang'};
    my $text  = set_noPunctuation($text_offset->{text});
    warn "Req --> ", $text;
    my @answer = Yahoo::Search->Results (
        Doc          => $text,
        AppId        => get_pref('Yahoo_key'),
        Mode         => 'all',
        Start        => 0,
        Count        => $count,
        Type         => 'all',
        AllowAdult   => 0,
        AllowSimilar => 0,
        Language     => $lang,
    );
    my @results;
    for my $Result (@answer) {
        my %text_off = %$text_offset;
        $text_off{summary} = ucfirst(lc($Result->Summary));
        my %data;
        $data{url}        = $Result->Url;
        $data{title}      = $Result->Title;
        @{$data{text_offset}} = (\%text_off,);
        $data{nb}         = 1;
        push @results, \%data;
    }

    return \@results;
}


# TODO fonction a finir....
sub _req_google {
    my $text  = shift;
    my $count = shift; # unused
    my $lang  = shift;

    my $ua = LWP::UserAgent->new;
    $ua->agent("no_plagia");

    # Create a request
    my $req = HTTP::Request->new(POST => ' http://www.google.fr/search');
    $req->content_type('application/x-www-form-urlencoded');
    $req->content("hl=$lang&q=$text&G=Recherche+Google&meta=");

    # Pass request to the user agent and get a response back
    my $res = $ua->request($req);

    # Check the outcome of the response
    if ($res->is_success) {
        warn $res->content;
    } else {
        warn $res->status_line, "\n";
    }
}

END { }
1;
__END__

=back

=head1 AUTEUR

    noplagia dev team

    http://noplagia.org

=cut
